﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Security.Cryptography;
using System.Data;
using System.ComponentModel;
using Entities;
using Datalayer;
using System.Net;
using System.Net.Mail;

namespace Common
{
    public static class MethodCommon
    {
        public static string Md5AddSecret(string strChange)
        {
            byte[] pass = Encoding.ASCII.GetBytes(strChange);
            MD5 md5 = new MD5CryptoServiceProvider();
            string strPassword = Encoding.ASCII.GetString(md5.ComputeHash(pass));
            return strPassword;
        }
        public static Guid Get_Guid(string str_guid)
        {
            Guid gd = new Guid(str_guid);
            return gd;
        }
        public static DataTable ToDataTable<T>(this IList<T> list)
        {
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            for (int i = 0; i < props.Count; i++)
            {
                PropertyDescriptor prop = props[i];
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            }
            object[] values = new object[props.Count];
            foreach (T item in list)
            {
                for (int i = 0; i < values.Length; i++)
                    values[i] = props[i].GetValue(item) ?? DBNull.Value;
                table.Rows.Add(values);
            }
            return table;
        }
        public static String StrQueryNotes(string QStrName, string QStrValue)
        {
            string s = "";
            if (QStrValue != null && QStrValue != null)
            {
                if (QStrName == "StrEdit")
                {
                    s = "Thay đổi thông tin";
                    if (QStrValue == "true")
                        s = s + " thành công";
                    else
                        s = s + " thất bại";
                }
                if (QStrName == "StrDel")
                {
                    s = "Xóa thông tin";
                    if (QStrValue == "true")
                        s = s + " thành công";
                    else
                        s = s + " thất bại";
                }
                if (QStrName == "StrNew")
                {
                    s = "Thêm mơi thông tin";
                    if (QStrValue == "true")
                        s = s + " thành công";
                    else
                        s = s + " thất bại";
                }
            }
            return s;
        }
        public static String StrQueryNotes_(string QStrout, string QStrValue)
        {
            string s = "";
            string o = "";
            if (QStrValue != null && QStrValue != null)
            {
                if (QStrValue == "true")
                    o = "thành công";

                else
                    o = "thất bại";
                s = QStrout + " "+ o;
            }
            return s;
        }
        public static bool IsHaveUserName(string strUsername)
        {
            bool ok = true;
            IQueryMgr iq = QueryMgr.GetDbInstance();
            User ur = new User();

            ur = iq.UR_GetUserByUserName(strUsername);
            if (ur == null)
                ok = false;
            return ok;
        }
        public static bool SendMail(string to, string subject, string message)
        {
            string gMailAccount = "47vn.fin@gmail.com";
            string password = "quangtinhdl";
            try
            {
                NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(gMailAccount);
                msg.To.Add(new MailAddress(to));
                msg.Subject = subject;
                msg.Body = message;
                msg.IsBodyHtml = true;
                SmtpClient client = new SmtpClient("smtp.gmail.com");
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = loginInfo;
                client.Send(msg);

                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

    }
}